package com.jx.driving.message;

import java.io.Serializable;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jx.driving.util.ByteUtils;

/**
 * 消息体属性
 * 
 * @author wuxiaozeng
 *
 */
public class MessageBodyProperty implements Serializable {

	private static final Logger logger = LoggerFactory.getLogger(MessageBodyProperty.class);

	private Integer bodyLength;// 消息体长度
	private String encrypt;// 数据加密方式
	private String other;// 保留
	private Boolean subPackageFlag = false;// 分包

	public MessageBodyProperty() {
		super();
	}

	/**
	 * 构造方法，将byte转换成对象
	 * 
	 */
	public MessageBodyProperty(byte[] bytes) {
		String binary = ByteUtils.byteToBit(bytes[0]).concat(ByteUtils.byteToBit(bytes[1]));
		if ('1' == binary.charAt(5)) {// 说明加密了
			this.encrypt = "RSA";
		}
		if (binary.charAt(2) == '1') {// 说明分包了
			this.subPackageFlag = true;
		}
		int s = Integer.parseInt(binary.substring(6), 2);
		this.bodyLength = s;
	}

	/**
	 * 得到消息体属性byte值,会返回byte[4],取后两位
	 */
	public byte[] getMessageBodyPropertyByteArray(byte[] body) {
		Integer length = body.length;// 消息体长度
		// 返回无符号的2进制表示 1110011
		String binary = Integer.toBinaryString(length);
		StringBuilder sb = new StringBuilder();
		sb.append("00");// 保留
		if (subPackageFlag) {// 分包
			sb.append("1");
		} else {
			sb.append("0");
		}
		if ("RSA".equals(getEncrypt())) {// 数据加密方式
			sb.append("001");
		} else {
			sb.append("000");
		}
		if (binary.length() < 10) {// 空位补0
			for (int i = 0; i < 10 - binary.length(); i++) {
				sb.append("0");
			}
		}
		sb.append(binary);
		int s = Integer.parseInt(sb.toString(), 2);// 消息体属性为int类型
		return ByteUtils.intToBytes(s);
	}

	public String getEncrypt() {
		return encrypt;
	}

	public void setEncrypt(String encrypt) {
		this.encrypt = encrypt;
	}

	public String getOther() {
		return other;
	}

	public void setSubPackageFlag(Boolean subPackageFlag) {
		this.subPackageFlag = subPackageFlag;
	}

	public void setOther(String other) {
		this.other = other;
	}

	public Boolean getSubPackageFlag() {
		return subPackageFlag;
	}

	public Integer getBodyLength() {
		return bodyLength;
	}

	public void setBodyLength(Integer bodyLength) {
		this.bodyLength = bodyLength;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((bodyLength == null) ? 0 : bodyLength.hashCode());
		result = prime * result + ((encrypt == null) ? 0 : encrypt.hashCode());
		result = prime * result + ((other == null) ? 0 : other.hashCode());
		result = prime * result + ((subPackageFlag == null) ? 0 : subPackageFlag.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MessageBodyProperty other = (MessageBodyProperty) obj;
		if (bodyLength == null) {
			if (other.bodyLength != null)
				return false;
		} else if (!bodyLength.equals(other.bodyLength))
			return false;
		if (encrypt == null) {
			if (other.encrypt != null)
				return false;
		} else if (!encrypt.equals(other.encrypt))
			return false;
		if (this.other == null) {
			if (other.other != null)
				return false;
		} else if (!this.other.equals(other.other))
			return false;
		if (subPackageFlag == null) {
			if (other.subPackageFlag != null)
				return false;
		} else if (!subPackageFlag.equals(other.subPackageFlag))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "MessageBodyProperty [bodyLength=" + bodyLength + ", encrypt=" + encrypt + ", other=" + other
				+ ", subPackageFlag=" + subPackageFlag + "]";
	}

	public static void main(String[] args) {
		// 消息体属性
		MessageBodyProperty property = new MessageBodyProperty();
		property.setEncrypt("RSA");
		property.setSubPackageFlag(true);
		byte[] propertyByte = property.getMessageBodyPropertyByteArray(new byte[1023]);
		System.out.println(Arrays.toString(propertyByte));
		System.out.println(propertyByte.length);
		MessageBodyProperty p = new MessageBodyProperty(new byte[] { propertyByte[2], propertyByte[3] });
		System.out.println(p);
	}

}
